/*
This code is copyright 2009-2010 by TMLA INC.  ALL RIGHTS RESERVED.
Please view license.txt in /tgsf_core/legal/license.txt or
http://tgWebSolutions.com/opensource/tgsf/license.txt
for complete licensing information.
*/

$(document).ready(
function()
{
	var dialog;
	if ( $("#dialog").length == 0 )
	{
        dialog = $('<div id="dialog" style="display:none;"></div>').appendTo('body');
	}
	else
	{
		dialog = $("#dialog");
	}
	dialog.dialog( {modal: true,autoOpen: false,width: 500, title: 'Add/Update Link' } );
	//------------------------------------------------------------------------
	$( 'form#new-link-form #cancel' ).click( function( event )
	{
		return tgsf.URL( '' ).redirect();
	});
	//------------------------------------------------------------------------
	$( 'form#new-link-form #link_url' ).focus();

	$( '.msgbox' ).delay( 10000 ).hide( 300 );

	$('.spamlink').click(
	function()
	{
		var link_id = $(this).attr('link_id');
		$.get( tgsf.URL( 'ajax/markspam' ).toString(), { i: link_id },
		   function( data )
			{
				$( '#ajax-msg' ).clearQueue();
				if ( data == 'remove' )
				{
					$( 'ul li.link[link_id|=' + link_id + ']' ).hide( 300 );
					$( '#ajax-msg' ).html( "Link was removed... " ).show(200).delay( 3000 ).hide( 300 );
				}
				
				else if ( data == 'previously-flagged' )
				{
					$( '#ajax-msg' ).html( "Multiple Spam Report Ignored..." ).show(200).delay( 5000 ).hide( 300 );
				}
				
				else if ( data == 'flagged' )
				{
					$( '#ajax-msg' ).html( "Thanks for letting us know..." ).show(200).delay( 3000 ).hide( 300 );
					
					var sc_span = $( 'ul li.link[link_id|=' + link_id + '] span.spam_count' );
					sc_span.html( parseInt( sc_span.html() ) + 1 );
				}
				
				else
				{
					$( '#ajax-msg' ).html( data ).show(200).delay( 3000 ).hide( 300 );
				}
			});
	});
	
	//------------------------------------------------------------------------
	$('#add-link').live( 'click',
	function()
	{
		$('#ajax-msg' ).hide();
	    var url = this.href + " #inner-page-content";

		dialog.attr( 'href', url );
		dialog.load
		(
			url,
			{},
			function(responseText, textStatus, XMLHttpRequest)
			{
				dialog.dialog( "open" );
				$('#dialog  #cancel').unbind( 'click' );
			}
		);

	    //do not follow link
	    return false;
	});

	$('#dialog  #cancel').live( 'click',
	function()
	{
		dialog.dialog( 'close' );
	});	
	//------------------------------------------------------------------------
	$('#dialog form').live( 'submit',
	function()
	{
		var url = $('#dialog form').attr( 'action' );
		var json = $('#dialog form').serialize();

		$.post
		(
			url,
			json,
			function( data, jqXHR )
			{
				// remove any error messages from a previous submission
				$('#dialog label').each(
				function()
				{
					$(this).find( 'span' ).remove();
					$(this).removeClass( 'errorCaption' );
				});
				
				// handle the form submission's results
				try
				{
					// handle errors
					if ( data.ajaxerror == true )
					{
						//errors come back as an assoc array (from php) into an object (in js - via json)
						for ( var key in data.errors )
						{
							var label = $( "label[for='" + key + "']" );
							label.html( label.html() + '<span class="error_message">' + data.errors[key] + '</span>' );
							label.addClass( 'errorCaption' );
						}
					}
					else if ( data.m.length > 0 )
					{
						// data.m is the short message code that's set on the rediected urls
						$.ajax({
						  url: tgsf.URL( 'ajax/get_message' ).setVar( 'm', data.m ),
						  success:
							function( longMessage )
							{
								$( '#inner-page-content' ).load( tgsf.URL( '' ) + ' #inner-page-content', null,
								function( data )
								{
									$('#ajax-msg' ).html( '<span></span>' + longMessage ).show();
								});
								
							}
						});
						dialog.dialog( 'close' );
					}
					else
					{
						$("#dialog").dialog( 'close' );
					}
				}
				catch( e )
				{
					dialog.dialog( 'close' );
				}
			}
		);
		return false;
	});
});